﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;
using System.Transactions;

namespace HanGu.Services
{
    public class ModuleService : BaseService, IModule
    {

        public List<Module> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Module.OrderBy(c => c.Parent).ThenBy(c => c.Order).ToList();
            }
        }

        public List<Module> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.Module.Where(c => 1 == 1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Title.IndexOf(searchparma.Ttile) >= 0);
                }
                if (searchparma.IntObj1 != 0)
                {
                    // query = query.Where(c => c.Parent == searchparma.IntObj1 || c.ID == searchparma.IntObj1);
                }
                query = base.Paging<Module, int?>(c => c.Order, query, searchparma.Pagination);
                return query.ToList();
            }
        }

        public Module GetByID(string id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Module.Where(c => c.ID == id).FirstOrDefault();
            }
        }


        public bool Add(Module model)
        {
            try
            {
                this.Add<Module>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Update(Module model)
        {
            try
            {
                this.Update<Module>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Delete(string id)
        {
            try
            {
                this.Delete<Module>(c => c.ID == id);
                return true;
            }
            catch { return false; }
        }

        public bool SaveAll(List<Module> models)
        {
           //using (TransactionScope scope = new TransactionScope()) 
            {
                using (HaiGuEntities dbConn = new HaiGuEntities())
                {
                    string sql = "delete from Module";
                    dbConn.ExecuteStoreCommand(string.Format(sql));
                    foreach (var item in models)
                    {
                        item.GUID = string.IsNullOrEmpty(item.GUID) ? Guid.NewGuid().ToString() : item.GUID;
                        dbConn.AddObject(item.GetType().Name, item);                        
                    }
                    dbConn.SaveChanges();
                    //scope.Complete();
                }
            }
            return true;

        }

    }
}
